import time

from view.ths_view.home_page_view import HomePageView
from appium.webdriver.common.appiumby import AppiumBy
from common import logger

"""
ths交易页面
"""


class TransactionView(HomePageView):
    # 交易tab
    tab_transaction = (
        AppiumBy.XPATH, "//android.widget.TextView[@resource-id='com.hexin.plat.android:id/title' and @text='交易']")

    # A股tab
    tab_a = (AppiumBy.ID, "com.hexin.plat.android:id/tab_a")
    # 港美股tab
    tab_gm = (AppiumBy.ID, "com.hexin.plat.android:id/tab_gm")
    # 基金tab
    tab_fund = (AppiumBy.ID, "com.hexin.plat.android:id/tab_fund")
    # 模拟tab
    tab_mn = (AppiumBy.ID, "com.hexin.plat.android:id/tab_mn")

    # A股tab下元素
    a_close_button = (AppiumBy.ID, "com.hexin.plat.android:id/close_button")

    # 模拟tab下元素
    # 总资产
    capital_value_text = (AppiumBy.ID, "com.hexin.plat.android:id/capital_value_text")
    # 浮动盈亏
    yingkui_value_text = (AppiumBy.ID, "com.hexin.plat.android:id/yingkui_value_text")
    # 总市值
    shizhi_value_text = (AppiumBy.ID, "com.hexin.plat.android:id/shizhi_value_text")
    # 当日参考盈亏
    dryk_value_text = (AppiumBy.ID, "com.hexin.plat.android:id/dryk_value_text")

    # 买入按钮
    menu_buy = (AppiumBy.ID, "com.hexin.plat.android:id/menu_buy_text")
    # 卖出按钮
    menu_sale = (AppiumBy.ID, "com.hexin.plat.android:id/menu_sale_text")
    # 撤单按钮
    menu_withdrawal = (AppiumBy.ID, "com.hexin.plat.android:id/menu_withdrawal_text")
    # 持仓按钮
    menu_holdings = (AppiumBy.ID, "com.hexin.plat.android:id/menu_holdings_text")
    # 查询按钮
    menu_search = (AppiumBy.ID, "com.hexin.plat.android:id/menu_search_text")

    def click_buy_menu(self):
        """
        买入
        :return:
        """
        self.click_element(self.menu_buy)

    def click_sale_menu(self):
        """
        卖出
        :return:
        """
        self.click_element(self.menu_sale)

    def click_withdrawal_menu(self):
        """
        撤回
        :return:
        """
        self.click_element(self.menu_withdrawal)

    def click_holdings_menu(self):
        """
        持仓
        :return:
        """
        self.click_element(self.menu_holdings)

    def click_search_menu(self):
        """
        查询
        :return:
        """
        self.click_element(self.menu_search)

    def click_transaction_tab(self):
        """
        进入交易tab
        :return:
        """
        self.click_element(self.tab_transaction)

    def click_mn_tab(self):
        """
        进入模拟tab
        :return:
        """
        self.click_element(self.tab_mn)

    def click_a_tab(self):
        """
        进入A股tab
        :return:
        """
        self.click_element(self.tab_a)

    def check_a_zh(self):
        """
        检查并关闭A股账号关闭按钮
        :return:
        """
        a_zh_btn = self.find_element_by_wait(self.a_close_button, timeout=2)
        if a_zh_btn:
            self.driver.press_keycode(4)
            a_zh_btn.click()
        else:
            logger.warning('没有找到关闭按钮!')
            for i in range(2):
                if self.find_element_by_wait(self.tab_mn, timeout=2):
                    break
                else:
                    logger.warning('未到mn_tab页面，点击返回键!')
                    self.driver.press_keycode(4)
                    time.sleep(1)
